package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Botany;
import cn.wolfcode.crm.query.BotanyQueryObject;
import cn.wolfcode.crm.query.JsonResult;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IBotanyService;
import cn.wolfcode.crm.util.QiniuUtil;
import cn.wolfcode.crm.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import java.util.List;

/**
 * @author Administrator jetlag
 */
@Controller
@RequestMapping("botany")
public class BotanyController {

    @Autowired
    private IBotanyService service;

    @Autowired
    private ServletContext app;

    /**
     * 展示首页数据
     * @return
     * @throws Exception
     */
    @RequestMapping("view")
    public String list() throws Exception{
        return "botany";
    }

    /**
     * 按条件查询相关数据
     * @param qo
     * @return
     * @throws Exception
     */
    @RequestMapping("query")
    @ResponseBody
    public Object query(BotanyQueryObject qo) throws Exception{
        return service.query(qo);
    }

    /**
     *
     * @param entity 植物的一些参数
     * @param pic 图片名称
     * @return
     * @throws Exception
     */
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Botany entity,MultipartFile pic) throws Exception {
        JsonResult result = new JsonResult();

      //如果图片不等于空的时候   进行压缩图的写入跟源文件写入
        if (pic != null && pic.getSize() > 0) {
            String contextPath = app.getRealPath("upload");

            //图片已经存在了本地的upload地址里面了
            String upload = UploadUtil.upload(pic, contextPath);

            //下面的一些七牛云的操作
            String substring = upload.substring(8, upload.length());

            //七牛上传工具
            QiniuUtil upload1 = new QiniuUtil();
            String newPath = contextPath + "\\" + substring;
            //七牛云上传成功后的地址
            String upload2 = upload1.upload(substring, newPath);
            try {
                if (entity.getId() != null) {
                    entity.setPicture(upload2);
                    service.updateByPrimaryKey(entity);
                } else {
                    entity.setPicture(upload2);
                    service.insert(entity);
                }
                return result;
            } catch (Exception e) {
                e.printStackTrace();
                return result.setMsg("操作失败");
            }
        }
        return result.setMsg("操作失败");
    }

    /**
     * 删除
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) throws Exception{
        JsonResult result = new JsonResult();
        try {
            Botany botany = service.selectByPrimaryKey(id);
            String substring = botany.getPicture().substring(34, botany.getPicture().length());
            UploadUtil.deleteFile(app,substring);
            service.deleteByPrimaryKey(id);
            return result;
        } catch (Exception e){
            e.printStackTrace();
            return result.setMsg("操作失败");
        }
    }
}
